//
//  WInfoViewController.swift
//  iiatn
//
//  Created by mnine on 18/4/1.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD


class WInfoViewController: itBaseViewController {

    
    @IBOutlet var name: UITextField!
    
    @IBOutlet var email: UITextField!
    
    @IBOutlet var province: UITextField!
    
    @IBOutlet var city: UITextField!
    
    @IBOutlet var company: UITextField!
    
    @IBOutlet var address: UITextView!
    
    @IBOutlet var storebtn: UIButton!
    
    
    var texts = [UITextField]()
    var hasemail = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(self.handleTap(sender:))))
        
        setstyle()
        setdata()
        setdosth()
    }

    
    
    func setstyle(){
        texts.append(name)
        texts.append(email)
        texts.append(province)
        texts.append(city)
        texts.append(company)
        
        for text in texts {
            text.layer.borderWidth = 0.7
            text.layer.borderColor = UIColor.gray.cgColor
            text.width = SCREENW - 142
            text.layer.cornerRadius = text.height * 0.2
            text.layer.masksToBounds = true
            text.layer.rasterizationScale = UIScreen.main.scale
        }
        
        address.layer.borderWidth = 0.7
        address.layer.borderColor = UIColor.gray.cgColor
        address.width = SCREENW - 142
        address.layer.cornerRadius = address.height * 0.2
        address.layer.masksToBounds = true
        address.layer.rasterizationScale = UIScreen.main.scale
        
        storebtn.width = SCREENW - 52
        storebtn.layer.cornerRadius = storebtn.height * 0.2
        storebtn.layer.masksToBounds = true
        storebtn.layer.rasterizationScale = UIScreen.main.scale
    }
    
    func setdata(){
        WNetwork.shareNetworkTool.loadPerson(user_id: wuserid!){ (msg) in
            self.name.text = wname!
            self.email.text = wemail!
            self.province.text = wprovince!
            self.city.text = wcity!
            self.company.text = wcompany_name!
            self.address.text = waddress!
            if !(wemail?.isEmpty)! {
                print("email is not empty")
                self.email.isEnabled = false
                self.email.isUserInteractionEnabled = false
                self.hasemail = true
            }
        }
    }
    
    
    func setdosth(){
        storebtn.addTarget(self, action: #selector(trystore), for: .touchUpInside)
    }
    
    override func setupNav(){
        view.backgroundColor = UIColor.white
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func trystore(){
        var go = true
        
        if !email.text!.isEmpty && !Validate.email(email.text!).isRight {
            go = false
            SVProgressHUD.showError(withStatus: "邮箱格式不正确！")
        }
        
        if go {
           dostore()
        }
    
    }

    func dostore(){
        WNetwork.shareNetworkTool.updatePerson(hasemail: hasemail,user_id: wuserid!, name: name.text!, email: email.text!, province: province.text!, city: city.text!, company_name: company.text!, address: address.text!)
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) {self.navigationBackClick()}
    }
    
    /// 返回按钮
    func navigationBackClick() {
        
        if SVProgressHUD.isVisible() {
            SVProgressHUD.dismiss()
        }
        if UIApplication.shared.isNetworkActivityIndicatorVisible {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
        let nback = navigationController?.popViewController(animated: true)
        print("修改信息后自动返回！\(nback)")
        
    }
    
    func handleTap(sender: UITapGestureRecognizer) {
        if sender.state == .ended {
            for text in texts {
                text.resignFirstResponder()
            }
            address.resignFirstResponder()
        }
        sender.cancelsTouchesInView = false
    }

}
